package app;

import app.dao.RoleDao;
import app.dao.UserDao;
import app.entity.Role;
import app.entity.User;
import io.jooby.Jooby;
import io.jooby.avaje.inject.AvajeInjectModule;
import io.jooby.avaje.jsonb.AvajeJsonbModule;
import io.jooby.hikari.HikariModule;
import io.jooby.jdbi.JdbiModule;
import io.jooby.jdbi.TransactionalRequest;
import io.jooby.netty.NettyServer;
import org.jdbi.v3.core.Handle;

public class App extends Jooby {

    {
        //服务器配置
        install(new NettyServer());
        //数据库配置
        install(new HikariModule());
        //配置jdbi
        install(new JdbiModule());
        // DI
        install(AvajeInjectModule.of());
        //json配置
        install(new AvajeJsonbModule());
        //注册事务管理器 默认无效 置于所有route之前
        use(new TransactionalRequest());

        // MVC 注册到容器 MvcInjectModule
        install(new MvcInjectModule());
//        mvc(MyController.class);
//        mvc(Controller.class);

        get("/txn", ctx -> {
            Handle handle = require(Handle.class);
            UserDao userDao = handle.attach(UserDao.class);
            RoleDao roleDao = handle.attach(RoleDao.class);
            var index = 1;
            //保存用户1
            User user = new User();
            user.setName("walden" + index);
            user.setAge(40 + index);
            user.setMobile("18001231559");
            user.setRemark("remark" + index);
            userDao.insert(user);
            int txn=1/0;
            //保存角色1
            Role role = new Role();
            role.setName("角色" + index);
            role.setCode("role" + index);
            role.setRemark("remark" + index);
            roleDao.insert(role);
            return "ok";
        });

    }

    public static void main(final String[] args) {
        runApp(args, App::new);
    }


}
